package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 发日记adapter
 * Created by wjy on 2019/1/15.
 */
public class ImageListItemAdapter extends BaseListAdapter<String, ImageListItemAdapter.ViewHolder> implements View.OnClickListener {

    private int mItemWidth;

    public ImageListItemAdapter(Context context, CopyOnWriteArrayList data, int itemWidth) {
        super(context, data);
        mItemWidth = itemWidth;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_public_daily, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String path = mData.get(position);
        holder.image.setTag(R.id.position, position);
        holder.delete.setTag(R.id.position, position);
        if (TextUtils.equals(path, "0")) {
            holder.delete.setVisibility(View.GONE);
            holder.image.setImageResource(R.mipmap.add_pic);
            holder.image.setOnClickListener(this);
        } else {
            holder.delete.setVisibility(View.VISIBLE);
            holder.image.setOnClickListener(null);
            if (!TextUtils.isEmpty(path) && !path.equals("null")) {
                Glide.with(mContext).load(path).into(holder.image);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag(R.id.position);
        if (mClickListener != null) {
            mClickListener.onItemClick(position, v.getId() == R.id.iv_delete);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        ImageView delete;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.iv_image);
            delete = itemView.findViewById(R.id.iv_delete);
            image.setOnClickListener(ImageListItemAdapter.this);
            delete.setOnClickListener(ImageListItemAdapter.this);
//            RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(mItemWidth, mItemWidth);
//            layoutParams1.setMargins(0,mItemWidth / 10,0,0);
//            itemView.setLayoutParams(layoutParams1);
            ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
            layoutParams.width = mItemWidth;
            layoutParams.height = mItemWidth;
            image.setLayoutParams(layoutParams);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, boolean isDelete);
    }

    private OnItemClickListener mClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }
}
